load("@rules_angular//src/architect:ng_config.bzl", "ng_config")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_library", "js_test")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//docs:defs.bzl", "ng_app")

package(default_visibility = ["//visibility:public"])

# Link npm packages
npm_link_all_packages(name = "node_modules")

# Root config files used throughout build/test/lint etc
copy_to_bin(
    name = "ng-base-config",
    srcs = [
        "package.json",
    ],
)

# Test config files
copy_to_bin(
    name = "ng-base-test-config",
    srcs = [
        "karma-custom-launchers.js",
    ],
)

ng_config(
    name = "config",
)

# The main application
ng_app(
    name = "app",
    project_name = "material-angular-io",
    deps = [
        "//docs:node_modules/@angular/components-examples",
        "//docs:node_modules/@stackblitz/sdk",
        "//docs:node_modules/moment",
        "//docs:node_modules/path-normalize",
        "//docs/src/assets/tokens",
    ],
)

alias(
    name = "serve",
    actual = ":build.serve",
)

js_library(
    name = "audit_lib",
    srcs = [
        "tools/audit-docs.js",
        "tools/lighthouse-audit.mjs",
    ],
    data = [
        "//docs:node_modules/light-server",
        "//docs:node_modules/lighthouse",
        "//docs:node_modules/lighthouse-logger",
        "//docs:node_modules/puppeteer-core",
        "//docs:node_modules/shelljs",
        "@rules_browsers//src/browsers/chromium",
    ],
)

js_binary(
    name = "audit_tool",
    data = [":audit_lib"],
    entry_point = "tools/audit-docs.js",
    env = {
        "CHROMIUM_BIN": "$(CHROME-HEADLESS-SHELL)",
    },
    toolchains = [
        "@rules_browsers//src/browsers/chromium:toolchain_alias",
    ],
)

js_test(
    name = "audit",
    args = [
        "$(location //docs:build.production)",
    ],
    data = [
        ":audit_lib",
        "//docs:build.production",
    ],
    entry_point = "tools/audit-docs.js",
    env = {
        "CHROMIUM_BIN": "$(CHROME-HEADLESS-SHELL)",
    },
    tags = ["audit"],
    toolchains = [
        "@rules_browsers//src/browsers/chromium:toolchain_alias",
    ],
)
